#include "menu.h"
#include "key4x4.h"
#include "oled.h"
#include "sg90.h"
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "string.h"
#include "as608.h"
uint8_t password[4] = { 1, 2, 3, 4 };        //??
uint8_t password_check[4] = { 0, 0, 0, 0 };  //??????
uint8_t open_flag = 0;  //?????
uint8_t password_error = 0;//???????

void menu_oled(void)
{
	
	  uint8_t num,num1;
		uint8_t i=0;
		vTaskDelay(pdMS_TO_TICKS(100));
		while(1){
			
			
			//vTaskDelay(pdMS_TO_TICKS(500));
			
		while(1){
			i=0;
			open_flag = 0;
			password_error = 0;
			OLED_ShowChinese(5,20,0,16,1);//?
			OLED_ShowChinese(22,20,1,16,1);//?
			OLED_ShowChinese(39,20,2,16,1);//?
			OLED_ShowChinese(56,20,3,16,1);//?
			OLED_ShowChinese(73,20,4,16,1);//?
			OLED_ShowChinese(90,20,5,16,1);//?
			OLED_ShowChinese(107,20,6,16,1);//?
			OLED_Refresh();	
			if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_13)==1)
			{
				if(press_FR()==0)
				{
					break;
				}
			}
			if(IC_test()==0)
			{
				break;
			}
			num = MAT_KEY_is_press();
			while(num == KEY_A)
			{
				open_flag = 0;
				password_error = 0;
				num1 = MAT_KEY_is_press();
				if(i==0)
				{
					vTaskDelay(pdMS_TO_TICKS(500));
					OLED_Clear();
					OLED_ShowChinese(25,10,7,16,1);//?
					OLED_ShowChinese(41,10,8,16,1);//?
					OLED_ShowChinese(57,10,9,16,1);//?
					OLED_ShowChinese(73,10,10,16,1);//?
          OLED_Refresh();
           }
					if(num1<=9)
					{
						password_check[i] = num1;
						OLED_ShowString(0 + 8 * i, 24, "*", 16, 1);
             OLED_Refresh();
              i++;
					}
					if(i==4)
					{
						uint8_t j=0;
						for(j=0;j<4;j++)
						{
							if(password[j]!=password_check[j])
							{
								
									password_error = 1;
									open_flag = 0;
									break;
								}else{
									open_flag = 1;
								}
								
							}
							
						}
					if(open_flag == 1)
					{
						vTaskDelay(pdMS_TO_TICKS(500));
						OLED_Clear();
						
            OLED_ShowString(0, 8, "OPEN DOOR", 16, 1);
            OLED_Refresh();
					  vTaskDelay(pdMS_TO_TICKS(500));
						DoorOpen();
					  vTaskDelay(pdMS_TO_TICKS(500));
             OLED_Clear();
						break;
					}
						if(password_error == 1)
						{
							vTaskDelay(pdMS_TO_TICKS(500));
							OLED_Clear();
                //OLED_ShowString(0, 8, "Password ERROR", 16, 1);
								OLED_ShowChinese(25,10,9,16,1);//?
								OLED_ShowChinese(41,10,10,16,1);//?
								OLED_ShowChinese(57,10,11,16,1);//?
								OLED_ShowChinese(73,10,12,16,1);//?							
                OLED_Refresh();
							vTaskDelay(pdMS_TO_TICKS(100));
							OLED_Clear();
							break;
							
						}
						if(num1==KEY_D)
						{
							OLED_Clear();
							break;
						}
						
					}
					while(num == KEY_B)
					{
						num1 = MAT_KEY_is_press();
            if (i == 0)
            {			
                OLED_Clear();
							//vTaskDelay(pdMS_TO_TICKS(100));
							
                //OLED_ShowString(0, 8, "Change password", 16, 1);
                OLED_ShowChinese(10,16,22,16,1);//?
								OLED_ShowChinese(26,16,23,16,1);//?
								OLED_ShowChinese(42,16,9,16,1);//?
								OLED_ShowChinese(58,16,10,16,1);//?
							OLED_Refresh();
            }
						 if (num1 <= 9)
            {
                password[i] = num1;
                OLED_ShowString(0 + 8 * i, 30, "*", 16, 1);
                OLED_Refresh();
                i++;
            }
            if (i > 3)
            {
                OLED_Clear();
                break;
            }
						if (num == KEY_D)
            {
							
             OLED_Clear();
                break;
            }
					}
				}
				
			}
		OLED_Refresh();
}
